package org.sanguine.study.hometasks.first.task.a;

import org.sanguine.study.hometasks.first.Runner;

public class TaskA6 extends CommonWordTask {
	private String foundWord;

	public static void main(String[] args) {
		Runner runner = new TaskA6();
		runner.run(args);
	}

	@Override
	public void doTask() {
		if (in == null || out == null || numberOfItems <= 0) return;
		readItems();
		if (items != null) findWord();
		printResult();
	}

	private void findWord() {
		for (String s : items) {
			if (isAscendingCharsWord(s)) {
				foundWord = s;
				break;
			}
		}
	}

	private boolean isAscendingCharsWord(String word) {
		if (word.isEmpty()) return false;
		
		char[] chars = word.toCharArray();
		char prevChar = chars[0];
		
		for (int i = 1; i < chars.length; i++) {
			if (chars[i] <= prevChar) return false;
			prevChar = chars[i];
		}
		return true;
	}

	private void printResult() {
		if (foundWord == null) out.println("Word not found");
		else out.printf("Found word: %s\n", foundWord);
	}

}
